package me.weijing.common.xmpp;

import java.io.IOException;

import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;

/**
 * An implementation of the SCRAM-SHA-1 SASL mechanism. This implementation is
 * based on Stroke http://swift.im/git/stroke.
 */
public class ScramSaslMechanism extends SASLMechanism {

    /**
     * The name of the SASL mechanism.
     */
    public static final String MECHANISM_NAME = "SCRAM-SHA-1";

    /**
     * Create a ScramSaslMechanism.
     * 
     * @param saslAuthentication
     *            the smack SASLAuthentication.
     * 
     */
    public ScramSaslMechanism(final SASLAuthentication saslAuthentication) {
        super(saslAuthentication);
    }

    @Override
    public void authenticate(String username, String host, String password) throws IOException, XMPPException {
        this.authenticationId = username;
        this.password = password;
        this.hostname = host;

        sc = new ScramSaslClient(username, this);
        authenticate();
    }

    @Override
    public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException {
        this.authenticationId = username;
        this.hostname = host;
        sc = new ScramSaslClient(username, cbh);
        authenticate();
    }

    @Override
    protected String getName() {
        return MECHANISM_NAME;
    }

}
